<?php

class Controller {
   public $load;
   public $model;

   function __construct()
   {
      $this->load = new Load();
      $this->model = new Model();
      $this->model->newConnection( "localhost", "progin", "progin", "progin_171_13509027" );
   }

   public function home($_state)
   {
		$data = array();
		$data['source'] = "home";
		$state = $_state;
		
		$data['home'] = "<li id='current-menu'> <a href='?page=home&state=1'> <span> <span> Home </span> </span> </a> </li>";
		$data['content'] = "<li> <a href='?page=contents&state=1&p=1'> <span> <span> Content </span> </span> </a> </li>";

		$usr = $_SESSION['user'];
		$query_avatar = "SELECT avatar FROM user WHERE username='$usr'";
		$avatar_id = $this->model->cacheQuery($query_avatar);
		$row = 0;
		$_user="";
		while($temp = $this->model->resultsFromCache( $avatar_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$_user = $arr_row;
			$row++;
		}
						
		if(isset($_SESSION['user'])) {
			$data['log'] = "<li><a href='?page=profil&p=1'> <span> <span> <img id='avatar-current' src='".$_user['avatar']."'> <span id='user-current'>".$_SESSION['user']."</span></span></span> </a></li>";
			$data['reg'] = "<li><a href='#'> <span> <span>  Logout  </span> </span> </a></li>";
			$data['hpost'] = "<li> <a href='?page=home&state=1'> <span> <span> Post </span> </span> </a> </li>";
		} else { 
			$data['log'] = "<li onclick='pop()'><a> <span> <span> Login </span> </span> </a></li>";
			$data['reg'] = "<li><a href='register.php'> <span> <span> Register</span> </span> </a></li>";				
			$data['hpost'] = "<li> <a href='register.php'> <span> <span> Post </span> </span> </a> </li>";
		}		

		$query_like = 'SELECT t1.ID_post, t1.judul, t1.wujud, t1.link, t1.deskripsi, UNIX_TIMESTAMP(t1.waktu) AS time, (like_count - dislike_count) AS total_like, t5.username, jml_komentar  FROM (
		(SELECT * FROM post) AS t1 INNER JOIN
		(SELECT ID_post,COUNT(*) AS like_count FROM ngelike WHERE number_like=1 GROUP BY ID_post) AS t2 INNER JOIN
		(SELECT ID_post,COUNT(*) AS dislike_count FROM ngelike WHERE number_like=-1  GROUP BY ID_post) AS t3 INNER JOIN
		(SELECT * FROM ngepost) AS t4 INNER JOIN
		(SELECT username FROM user) AS t5 INNER JOIN
		(SELECT ID_post, COUNT(*) AS jml_komentar FROM dikomentar GROUP BY ID_post) AS t6
		ON t1.ID_post=t2.ID_post AND t2.ID_post=t3.ID_post AND t3.ID_post=t4.ID_post AND t4.username=t5.username AND t1.ID_post=t6.ID_post
		) ORDER BY total_like DESC LIMIT 0,3';
		
		$query_comment = 'SELECT t1.ID_post, t1.judul, t1.wujud, t1.link, t1.deskripsi, UNIX_TIMESTAMP(t1.waktu) AS time, (like_count - dislike_count) AS total_like, t5.username, jml_komentar  FROM (
		(SELECT * FROM post) AS t1 INNER JOIN
		(SELECT ID_post,COUNT(*) AS like_count FROM ngelike WHERE number_like=1 GROUP BY ID_post) AS t2 INNER JOIN
		(SELECT ID_post,COUNT(*) AS dislike_count FROM ngelike WHERE number_like=-1  GROUP BY ID_post) AS t3 INNER JOIN
		(SELECT * FROM ngepost) AS t4 INNER JOIN
		(SELECT username FROM user) AS t5 INNER JOIN
		(SELECT ID_post, COUNT(*) AS jml_komentar FROM dikomentar GROUP BY ID_post) AS t6
		ON t1.ID_post=t2.ID_post AND t2.ID_post=t3.ID_post AND t3.ID_post=t4.ID_post AND t4.username=t5.username AND t1.ID_post=t6.ID_post
		) ORDER BY jml_komentar DESC LIMIT 0,3';
		
		$posts1_id = $this->model->cacheQuery($query_like);
		$posts2_id = $this->model->cacheQuery($query_comment);
		
		$post1 = array();
		$post2 = array();
		$row = 0;
		while($temp = $this->model->resultsFromCache( $posts1_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$post1[] = $arr_row;
			$row++;
		}
		$row = 0;
		while($temp = $this->model->resultsFromCache( $posts2_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$post2[] = $arr_row;
			$row++;
		}
		
		switch($state) {
			case 1 :
				$data['post'] = $post1;
				$data['state'] = 1;
				$data['tab'] = "<li><a class='current' href='index.php?page=home&state=1'>Three Most Popular</a></li><li><a class='' href='index.php?page=home&state=2'>Three Most Commented</a></li>";
			break;
			case 2:
				$data['post'] = $post2;
				$data['state'] = 2;
				$data['tab'] = "<li><a class='' href='index.php?page=home&state=1'>Three Most Popular</a></li><li><a class='current' href='index.php?page=home&state=2'>Three Most Commented</a></li>";
			break;
			default:
			break;
		}
	  
      $this->load->view('home_view.php', $data);
   }
   
   public function contents($_state, $_page)
   {
		$data = array();
		$data['source'] = "contents";
		$state = $_state;
		$data['state'] = $state;
		
		//header
		$data['home'] = "<li> <a href='?page=home&state=1'> <span> <span> Home </span> </span> </a> </li>";
		$data['content'] = "<li  id='current-menu'> <a href='?page=contents&state=1&p=1'> <span> <span> Content </span> </span> </a> </li>";

		$usr = $_SESSION['user'];
		$query_avatar = "SELECT avatar FROM user WHERE username='$usr'";
		$avatar_id = $this->model->cacheQuery($query_avatar);
		$row = 0;
		$_user="";
		while($temp = $this->model->resultsFromCache( $avatar_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$_user = $arr_row;
			$row++;
		}
						
		if(isset($_SESSION['user'])) {
			$data['log'] = "<li><a href='?page=profil&p=1'> <span> <span> <img id='avatar-current' src='".$_user['avatar']."'> <span id='user-current'>".$_SESSION['user']."</span></span></span> </a></li>";
			$data['reg'] = "<li><a href='#'> <span> <span>  Logout  </span> </span> </a></li>";
		} else { 
			$data['log'] = "<li><a href='#'> <span> <span> Login </span> </span> </a></li>";
			$data['reg'] = "<li><a href='register.php'> <span> <span> Register</span> </span> </a></li>";
			$data['hpost'] = "<li> <a href='?page=home&state=1'> <span> <span> Post </span> </span> </a> </li>";				
		}
		
		$startpage = ($_page-1)*7;
		
		$query = "SELECT id, judul, wujud, link, deskripsi, time, total_like, username, jml_komentar FROM (
		(
			SELECT id,judul, wujud, link, deskripsi, time, (like_count - t3.dislike_count) AS total_like FROM (
				(SELECT t1.ID_post AS id, t1.judul AS judul, t1.wujud AS wujud, t1.link AS link, t1.deskripsi AS deskripsi, UNIX_TIMESTAMP(t1.waktu) AS time, like_count FROM
					(
						(SELECT * FROM post) AS t1 LEFT JOIN
						(SELECT ID_post,COUNT(*) AS like_count FROM ngelike WHERE number_like=1 GROUP BY ID_post) AS t2
						ON t1.ID_post=t2.ID_post
					)
				) AS t7 LEFT JOIN 
					(SELECT ID_post,COUNT(*) AS dislike_count FROM ngelike WHERE number_like=-1 GROUP BY ID_post) AS t3 
				ON id=t3.ID_post
			)
		) AS t8 LEFT JOIN (
			SELECT t10.ID_post, username, jml_komentar FROM (
				(
					SELECT t4.ID_post, t5.username AS username FROM
					(
						(SELECT * FROM ngepost) AS t4 LEFT JOIN 
						(SELECT username FROM user) AS t5
						ON t4.username=t5.username
					) 
				) AS t10 LEFT JOIN
					(SELECT ID_post, COUNT(*) AS jml_komentar FROM dikomentar GROUP BY ID_post) AS t6
					ON t10.ID_post=t6.ID_post
			)
		) AS t9 
		ON t9.ID_post=id
	) ";
		
		switch($_state) {
			case 1 :
				$query .= "ORDER BY total_like DESC";
				$data['tab'] = "<li><a class='current' href='?page=contents&state=1&p=1'>Most Popular</a></li><li><a class='' href='?page=contents&state=2&p=1'>Most Commented</a></li><li><a class='' href='?page=contents&state=3&p=1'>Newest</a></li>";
			break;
			case 2 :
				$query .= "ORDER BY jml_komentar DESC";
				$data['tab'] = "<li><a class='' href='?page=contents&state=1&p=1'>Most Popular</a></li><li><a class='current' href='?page=contents&state=2&p=1'>Most Commented</a></li><li><a class='' href='?page=contents&state=3&p=1'>Newest</a></li>";
			break;
			case 3 :
				$query .= "ORDER BY time DESC";
				$data['tab'] = "<li><a class='' href='?page=contents&state=1&p=1'>Most Popular</a></li><li><a class='' href='?page=contents&state=2&p=1'>Most Commented</a></li><li><a class='current' href='?page=contents&state=3&p=1'>Newest</a></li>";
			break;
			default:
			break;
		}
		
		$queryTag = "SELECT DISTINCT tag_name FROM ditag";
		
		$posts_id = $this->model->cacheQuery($query);
		$tag_id = $this->model->cacheQuery($queryTag);
	
		$post = array();
		$tag = array();
		
		$row = 0;
		while($temp = $this->model->resultsFromCache( $posts_id ) ) {
			if($row>=$startpage && $row < $startpage+7) {
				$arr_row = array();
				foreach( $temp as $col => $val ) {
					if(!$val)
						$arr_row[$col] = 0; 
					else
						$arr_row[$col] = $val; 
				}
				$post[] = $arr_row;
			}
			$row++;
		}
		while($temp = $this->model->resultsFromCache( $tag_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$tag[] = $arr_row;
			$row++;
		}
		
		$data['state'] = $state;
		$data['post'] = $post;
		$data['npage'] = ceil($this->model->numRowsFromCache( $posts_id )/7);
		$data['ncontent'] = count($post);
		$data['tag']= $tag;
		$data['ntag']= count($tag);
	
      $this->load->view('listcontent_view.php', $data);
   }
   
   public function content($_id, $_page)
   {
		$data = array();
		$data['source'] = "content";
		
		//header
		$data['home'] = "<li> <a href='?page=home&state=1'> <span> <span> Home </span> </span> </a> </li>";
		$data['content'] = "<li  id='current-menu'> <a href='?page=contents&state=1&p=1'> <span> <span> Content </span> </span> </a> </li>";

		$usr = $_SESSION['user'];
		$query_avatar = "SELECT avatar FROM user WHERE username='$usr'";
		$avatar_id = $this->model->cacheQuery($query_avatar);
		$row = 0;
		$_user="";
		while($temp = $this->model->resultsFromCache( $avatar_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$_user = $arr_row;
			$row++;
		}
						
		if(isset($_SESSION['user'])) {
			$data['log'] = "<li><a href='?page=profil&p=1'> <span> <span> <img id='avatar-current' src='".$_user['avatar']."'> <span id='user-current'>".$_SESSION['user']."</span></span></span> </a></li>";
			$data['reg'] = "<li><a href='#'> <span> <span>  Logout  </span> </span> </a></li>";
		} else { 
			$data['log'] = "<li><a href='#'> <span> <span> Login </span> </span> </a></li>";
			$data['reg'] = "<li><a href='register.php'> <span> <span> Register</span> </span> </a></li>";
			$data['hpost'] = "<li> <a href='?page=home&state=1'> <span> <span> Post </span> </span> </a> </li>";				
		}
		
		$startpage = ($_page-1)*7;
		
		$queryContent = "SELECT id, judul, wujud, link, dekripsi, time, total_like, username, jml_komentar FROM (
		(
			SELECT id,judul, wujud, link, dekripsi, time, (like_count - t3.dislike_count) AS total_like FROM (
				(SELECT t1.ID_post AS id, t1.judul AS judul, t1.wujud AS wujud, t1.link AS link, t1.deskripsi AS dekripsi, UNIX_TIMESTAMP(t1.waktu) AS time, like_count FROM
					(
						(SELECT * FROM post) AS t1 LEFT JOIN
						(SELECT ID_post,COUNT(*) AS like_count FROM ngelike WHERE number_like=1 GROUP BY ID_post) AS t2
						ON t1.ID_post=t2.ID_post
					)
				) AS t7 LEFT JOIN 
					(SELECT ID_post,COUNT(*) AS dislike_count FROM ngelike WHERE number_like=-1 GROUP BY ID_post) AS t3 
				ON id=t3.ID_post
			)
		) AS t8 LEFT JOIN (
			SELECT t10.ID_post, username, jml_komentar FROM (
				(
					SELECT t4.ID_post, t5.username AS username FROM
					(
						(SELECT * FROM ngepost) AS t4 LEFT JOIN 
						(SELECT username FROM user) AS t5
						ON t4.username=t5.username
					) 
				) AS t10 LEFT JOIN
					(SELECT ID_post, COUNT(*) AS jml_komentar FROM dikomentar GROUP BY ID_post) AS t6
					ON t10.ID_post=t6.ID_post
			)
		) AS t9 
		ON t9.ID_post=id
	) WHERE id=$_id";
		
		$queryComment = "SELECT u.username, u.avatar, UNIX_TIMESTAMP(k.waktu) as waktu, k.ID_komentar as id, k.isi FROM 
		user AS u INNER JOIN dikomentar AS d INNER JOIN komentar AS k
			ON u.username=d.username AND d.ID_komentar=k.ID_komentar
		WHERE d.ID_post=$_id";
		
		$post_id = $this->model->cacheQuery($queryContent);
		$comment_id = $this->model->cacheQuery($queryComment);
	
		$post = "";
		$comment = array();
		
		$row = 0;
		while($temp = $this->model->resultsFromCache( $post_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$post = $arr_row;
			$row++;
		}
		$row = 1;
		while($temp = $this->model->resultsFromCache( $comment_id ) ) {
			if($row>=$startpage && $row < $startpage+7) {
				$arr_row = array();
				foreach( $temp as $col => $val ) {
					$arr_row[$col] = $val; 
				}
				$comment[] = $arr_row;
			}
			$row++;
		}
		
		$data['npage'] = ceil(($this->model->numRowsFromCache( $comment_id ))/7);
		$data['post'] = $post;
		$data['comment']= $comment;
		$data['ncomment']= count($comment);
	
      $this->load->view('content_view.php', $data);
   }
   
   public function profile() {
		
		$data = array();
		$data['source'] = "profile";
		$state = $_state;
		$usr = $_SESSION['user'];
		
		$data['home'] = "<li> <a href='?page=home&state=1'> <span> <span> Home </span> </span> </a> </li>";
		$data['content'] = "<li> <a href='?page=contents&state=1&p=1'> <span> <span> Content </span> </span> </a> </li>";

		$usr = $_SESSION['user'];
		$query_avatar = "SELECT avatar FROM user WHERE username='$usr'";
		$avatar_id = $this->model->cacheQuery($query_avatar);
		$row = 0;
		$_user="";
		while($temp = $this->model->resultsFromCache( $avatar_id ) ) {
			$arr_row = array();
			foreach( $temp as $col => $val ) {
				$arr_row[$col] = $val; 
			}
			$_user = $arr_row;
			$row++;
		}
						
		if(isset($_SESSION['user'])) {
			$data['log'] = "<li id='current-menu'><a href='?page=profil&p=1'> <span> <span> <img id='avatar-current' src='".$_user['avatar']."'> <span id='user-current'>".$_SESSION['user']."</span></span></span> </a></li>";
			$data['reg'] = "<li><a href='#'> <span> <span>  Logout  </span> </span> </a></li>";
			$data['hpost'] = "<li> <a href='?page=home&state=1'> <span> <span> Post </span> </span> </a> </li>";				
		} else { 
			$data['log'] = "<li><a href='#'> <span> <span> Login </span> </span> </a></li>";
			$data['reg'] = "<li><a href='register.php'> <span> <span> Register</span> </span> </a></li>";
			$data['hpost'] = "<li> <a href='index.php'> <span> <span> Post </span> </span> </a> </li>";				
		}
		
		$query_post = "SELECT id, judul, wujud, link, deskripsi, time, total_like, username, jml_komentar FROM (
		(
			SELECT id,judul, wujud, link, deskripsi, time, (like_count - t3.dislike_count) AS total_like FROM (
				(SELECT t1.ID_post AS id, t1.judul AS judul, t1.wujud AS wujud, t1.link AS link, t1.deskripsi AS deskripsi, UNIX_TIMESTAMP(t1.waktu) AS time, like_count FROM
					(
						(SELECT * FROM post) AS t1 LEFT JOIN
						(SELECT ID_post,COUNT(*) AS like_count FROM ngelike WHERE number_like=1 GROUP BY ID_post) AS t2
						ON t1.ID_post=t2.ID_post
					)
				) AS t7 LEFT JOIN 
					(SELECT ID_post,COUNT(*) AS dislike_count FROM ngelike WHERE number_like=-1 GROUP BY ID_post) AS t3 
				ON id=t3.ID_post
			)
		) AS t8 LEFT JOIN (
			SELECT t10.ID_post, username, jml_komentar FROM (
				(
					SELECT t4.ID_post, t5.username AS username FROM
					(
						(SELECT * FROM ngepost) AS t4 LEFT JOIN 
						(SELECT username FROM user) AS t5
						ON t4.username=t5.username
					) 
				) AS t10 LEFT JOIN
					(SELECT ID_post, COUNT(*) AS jml_komentar FROM dikomentar GROUP BY ID_post) AS t6
					ON t10.ID_post=t6.ID_post
			)
		) AS t9 
		ON t9.ID_post=id
	) WHERE username = '$usr'";
		
		$post_id = $this->model->cacheQuery($query_post);
		
		$post = array();
		$row = 0;
		while($temp = $this->model->resultsFromCache( $post_id ) ) {
			if($row>=$startpage && $row < $startpage+3) {
				$arr_row = array();
				foreach( $temp as $col => $val ) {
					$arr_row[$col] = $val; 
				}
				$post[] = $arr_row;
			}
			$row++;
		}
		
		$data['post'] = $post;
		$data['npage'] = ceil($this->model->numRowsFromCache( $posts_id )/3);
		$data['ncontent'] = count($post);
		
		$this->load->view('profile_view.php', $data);
   }
}
